package com.zzyl.nursing.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.constant.CacheConstants;
import com.zzyl.common.utils.StringUtils;
import com.zzyl.nursing.domain.DeviceData;
import com.zzyl.nursing.domain.Room;
import com.zzyl.nursing.mapper.RoomMapper;
import com.zzyl.nursing.service.IRoomService;
import com.zzyl.nursing.vo.BedVo;
import com.zzyl.nursing.vo.DeviceInfo;
import com.zzyl.nursing.vo.RoomVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 房间Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-26
 */
@Service
public class RoomServiceImpl extends ServiceImpl<RoomMapper, Room> implements IRoomService {
    @Autowired
    private RoomMapper roomMapper;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 查询房间
     *
     * @param id 房间主键
     * @return 房间
     */
    @Override
    public Room selectRoomById(Long id) {
        return getById(id);
    }

    /**
     * 查询房间列表
     *
     * @param room 房间
     * @return 房间
     */
    @Override
    public List<Room> selectRoomList(Room room) {
        return roomMapper.selectRoomList(room);
    }

    /**
     * 新增房间
     *
     * @param room 房间
     * @return 结果
     */
    @Override
    public int insertRoom(Room room) {
        return save(room) ? 1 : 0;
    }

    /**
     * 修改房间
     *
     * @param room 房间
     * @return 结果
     */
    @Override
    public int updateRoom(Room room) {
        return updateById(room) ? 1 : 0;
    }

    /**
     * 批量删除房间
     *
     * @param ids 需要删除的房间主键
     * @return 结果
     */
    @Override
    public int deleteRoomByIds(Long[] ids) {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 根据楼层 id 获取房间视图对象列表
     *
     * @param floorId
     * @return
     */
    @Override
    public List<RoomVo> getRoomsByFloorId(Long floorId) {
        return roomMapper.selectByFloorId(floorId);
    }

    /**
     * 获取所有房间（负责老人）
     *
     * @param floorId
     * @return
     */
    @Override
    public List<RoomVo> getRoomsWithNurByFloorId(Long floorId) {
        return roomMapper.selectByFloorIdWithNur(floorId);
    }

    /**
     * 根据id获取房间详细信息
     *
     * @param id
     * @return
     */
    @Override
    public RoomVo getOneInfo(Integer id) {
        return roomMapper.getOneInfo(id);
    }

    /**
     * 获取房间中的智能设备
     *
     * @return
     */
    @Override
    public List<RoomVo> getAllFloorsWithDevice(Long floorId) {
        List<RoomVo> voList = roomMapper.getAllFloorsWithDevice(floorId);
        //TODO 获取每个设备的最新数据
        //遍历每个房间
        if (!CollectionUtil.isEmpty(voList)) {
            for (RoomVo room : voList) {
                //得房间的设备
                List<DeviceInfo> roomDeviceList = room.getDeviceVos();
                //设置房间设备数据
                setDeviceDate(roomDeviceList);
                //每个房间下的床位,可能是多张床
                List<BedVo> bedList = room.getBedVoList();
                //遍历每个床位
                if (!CollectionUtil.isEmpty(bedList)) {
                    for (BedVo bed : bedList) {
                        List<DeviceInfo> bedDeviceList = bed.getDeviceVos();
                        //设置床位设备数据
                        setDeviceDate(bedDeviceList);
                    }
                }
            }
        }
        return voList;
    }

    /**
     * 从redis要最新的数据并设置设备数据
     * @param deviceList
     */
    private void setDeviceDate(List<DeviceInfo> deviceList){
        //可能是多个设备，遍历每个设备
        if (!CollectionUtil.isEmpty(deviceList)) {
            for (DeviceInfo roomDevice : deviceList) {
                String deviceId = roomDevice.getIotId();
                //找redis要最新的数据
                String deviceDataListJsonStr = (String)stringRedisTemplate.opsForHash().get(CacheConstants.IOT_DEVICE_LAST_DATA, deviceId);
                if(StringUtils.hasText(deviceDataListJsonStr)){
                    List<DeviceData> deviceDataList = JSONUtil.toList(deviceDataListJsonStr, DeviceData.class);
                    roomDevice.setDeviceDataVos(deviceDataList);
                }
            }
        }
    }

}
